<?php
/**
 * @since v1.0
 * @modify 2015.6.21
 * @copyright 深圳亿游
 */
require_once(LIB_PATH.'controller/root.con.php');

class traceCon extends rootCon
{
    //追踪
    public function listAction() {
        $uid = mylogin::getUid(true);

        $trace = new Visitor();
        //找到当前用户对应的机构id
        if(r('isDebug')) {
            $schoolId = 1;
            $sql = "SELECT * FROM ".$trace->table()." WHERE schoolId=".$schoolId;
        }else if(r('type')=='schoolAdmin'){
            $schoolAdmin = new SchoolAdmin();
            $schoolAdmin->uid= $uid;
            $schoolAdmin->status = STATUS_PUBLISH;
            $find = $schoolAdmin->find();
            if(!$find){
                $this->j(-1,"没有找到通过审核的机构",['uid'=>$uid]);
            }
            $schoolId = $schoolAdmin->schoolId;
            $sql = "SELECT * FROM ".$trace->table()." WHERE schoolId=".$schoolId;
        }else if(r('type')=='teacher'){
            $teacher = new Teacher();
            $find = $teacher->find(['uid'=>$uid,'status'=>STATUS_PUBLISH]);
            if(!$find){
                $this->j(-2,'您没有通过审核的老师身份',['uid'=>$uid]);
            }

            //检查这个老师，有哪些课程
            $teacherCourse = new CourseTeacher();
            $teacherCourse->teacherId = $teacher->teacherId;
            $teacherCourse->status = STATUS_PUBLISH;
            $all = $teacherCourse->findAll();
            $i = 0;
            $courseIds = [-1];
            foreach($all as $item){
                $courseIds[] = $item['courseId'];
                $i++;
            }
            $courseStr = join(",",$courseIds);
            $sql = "SELECT * FROM ".$trace->table()." WHERE courseId IN ($courseStr) OR teacherId=".$teacher->teacherId;
        }else{
            $this->j(-1,'参数错了',['type'=>r('type')]);
        }

        $rect = $trace->pager($sql);
        $i = 0;
        foreach($rect['list'] as $item){
            if(!$item['avatar']){
                $rect['list'][$i]['avatar'] = FRONT_URL."/default.png";
            }else{
                $rect['list'][$i]['avatar'] = FRONT_URL.$item['avatar'];
            }
            $i++;
        }

        $this->j(0,"",$rect);

    }


}



